import React, { useRef } from 'react'
import { Animated, Button, SafeAreaView, StyleSheet, Text, View } from 'react-native'

export default function Animated_01() {
  // 初始化动画
  const fadeAnim = useRef(new Animated.Value(0)).current;
  
  const fadeIn = () => {
    // 配置动画
    Animated.timing(fadeAnim, {
      toValue: 1, // 变量
      duration: 1500,
      useNativeDriver: true, // 启用原生动画驱动
    }).start();
  };

  const fadeOut = () => {
    Animated.timing(fadeAnim, {
      toValue: 0, // 变量
      duration: 1000,
      useNativeDriver: true,
    }).start();
  };

  return (
    <SafeAreaView style={styles.container}>
    <Animated.View
      style={[
        styles.fadingContainer,
        {
          opacity: fadeAnim,
        },
      ]}>
      <Text style={styles.fadingText}>this is View!</Text>
    </Animated.View>
    
    <View style={styles.buttonRow}>
      <Button title="载入view" onPress={fadeIn} />
      <Button title="移除view" onPress={fadeOut} />
    </View>
  </SafeAreaView>
  )
}
const styles = StyleSheet.create({
  container: {
    flex: 1,
    alignItems: 'center',
    justifyContent: 'center',
  },
  fadingContainer: {
    padding: 20,
    backgroundColor: 'powderblue',
  },
  fadingText: {
    fontSize: 28,
  },
  buttonRow: {
    flexBasis: 100,
    justifyContent: 'space-evenly',
    marginVertical: 16,
  },
});